package cn.com.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.entity.GasStationinfoForecastMonthEntity;
import cn.com.gas.service.GasStationinfoForecastMonthService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

/**
 * 加油站预估列表(月)
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-08-30 15:50:40
 */
@Controller
@RequestMapping("/gas/stationforecastmonth")
public class GasStationinfoForecastMonthController {
	@Autowired
	private GasStationinfoForecastMonthService gasStationinfoForecastMonthService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private GasStationinfoService stationinfoService;
	@Autowired
	private HttpSessionService httpSessionService;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	@ResponseBody
	public DataResult list(@RequestBody GasStationinfoForecastMonthEntity gasStationinfoForecastMonth) {
		String stationinfoName = gasStationinfoForecastMonth.getStationinfoName();
		List<String> stationinfoList = new ArrayList<String>();
		
		if(TsBqUtils.isNotNull(stationinfoName)){
			LambdaQueryWrapper<GasStationinfoEntity> queryWrapperGasStationinfo = Wrappers.lambdaQuery();
			queryWrapperGasStationinfo.like(GasStationinfoEntity::getName, stationinfoName);
			List<GasStationinfoEntity> list = stationinfoService.list(queryWrapperGasStationinfo);
			if(TsBqUtils.isNotNull(list)){
				for (GasStationinfoEntity gasStationinfoEntity : list) {
					stationinfoList.add(gasStationinfoEntity.getId());
					
				}
			}else{
				return  DataResult.fail("未查询到 "+stationinfoName+" 相关信息！");
			}
			
		}
		
		
		
		Page page = new Page(gasStationinfoForecastMonth.getPage(), gasStationinfoForecastMonth.getLimit());
		LambdaQueryWrapper<GasStationinfoForecastMonthEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		
		if(TsBqUtils.isNotNull(gasStationinfoForecastMonth.getTime())){
			queryWrapper.eq(GasStationinfoForecastMonthEntity::getTime, gasStationinfoForecastMonth.getTime());
			
		}
		
		if(TsBqUtils.isNotNull(gasStationinfoForecastMonth.getTaxorgcode())){
			String taxorgcode = gasStationinfoForecastMonth.getTaxorgcode();
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(taxorgcode))) {
				// 县区局
				queryWrapper.eq(GasStationinfoForecastMonthEntity::getTaxorgcode, taxorgcode);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(taxorgcode))) {
				// 市局
				queryWrapper.like(GasStationinfoForecastMonthEntity::getTaxorgcode, taxorgcode.substring(0, 5));
			} else {
				// 省局
			}
			
		}else{
			return DataResult.success("未选择税务机关");
		}
		
		if(TsBqUtils.isNotNull(stationinfoList)){
			queryWrapper.in(GasStationinfoForecastMonthEntity::getStationinfoId, stationinfoList);
			
		}
		
		IPage<GasStationinfoForecastMonthEntity> iPage = gasStationinfoForecastMonthService.page(page, queryWrapper);
		List<GasStationinfoForecastMonthEntity> records = iPage.getRecords();
		for (GasStationinfoForecastMonthEntity entity : records) {
			String taxorgcode = entity.getTaxorgcode();
			SysDept dept = deptService.getById(taxorgcode);
			if(TsBqUtils.isNotNull(dept)){
				entity.setTaxorgName(dept.getName());
				
			}
			
			String stationinfoId = entity.getStationinfoId();
			GasStationinfoEntity stationinfo = stationinfoService.getById(stationinfoId);
			if(TsBqUtils.isNotNull(stationinfo)){
				entity.setStationinfoName(stationinfo.getName());
				entity.setGasLocation(stationinfo.getGasLocation());
				entity.setGasCoverage(stationinfo.getGasCoverage());
				
				entity.setTaxpayerName(stationinfo.getTaxpayerName());
				entity.setTaxpayerCode(stationinfo.getTaxpayerCode());
			}
		}
		return DataResult.success(iPage);
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list0")
	@ResponseBody
	public DataResult list0(@RequestBody GasStationinfoForecastMonthEntity gasStationinfoForecastMonth) {
		
		String currentDeptId = httpSessionService.getCurrentDeptId();
		String taxorgcode = gasStationinfoForecastMonth.getTaxorgcode();
		
		Page page = new Page(gasStationinfoForecastMonth.getPage(), gasStationinfoForecastMonth.getLimit());
		LambdaQueryWrapper<SysDept> deptQueryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		if (TsBqUtils.isNotNull(taxorgcode)) {
			deptQueryWrapper.eq(SysDept::getId, taxorgcode);

		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				deptQueryWrapper.eq(SysDept::getId, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				deptQueryWrapper.like(SysDept::getId, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
		
		IPage<SysDept> iPage = deptService.page(page, deptQueryWrapper);
		List<SysDept> records = iPage.getRecords();
		
		for (SysDept entity : records) {
			String deptId = entity.getId();
			LambdaQueryWrapper<GasStationinfoForecastMonthEntity> queryWrapper = Wrappers.lambdaQuery();
			// 查询条件示例
			
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(deptId))) {
				// 县区局
				queryWrapper.eq(GasStationinfoForecastMonthEntity::getTaxorgcode, deptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(deptId))) {
				// 市局
				queryWrapper.like(GasStationinfoForecastMonthEntity::getTaxorgcode, deptId.substring(0, 5));
			} else {
				// 省局
			}
			
			if(TsBqUtils.isNotNull(gasStationinfoForecastMonth.getTime())){
				queryWrapper.eq(GasStationinfoForecastMonthEntity::getTime, gasStationinfoForecastMonth.getTime());
				
			}
			
			List<GasStationinfoForecastMonthEntity> list = gasStationinfoForecastMonthService.list(queryWrapper);
			BigDecimal forecastValueBd = new BigDecimal(0);
			BigDecimal gatherValueBd = new BigDecimal(0);
			BigDecimal amountDieselBd = new BigDecimal(0);
			BigDecimal amountGasolineBd = new BigDecimal(0);
			
			for (GasStationinfoForecastMonthEntity forecastMonthEntity : list) {
				String forecastValue = forecastMonthEntity.getForecastValue();
				String gatherValue = forecastMonthEntity.getGatherValue();
				String amountDiesel = forecastMonthEntity.getAmountDiesel();
				String amountGasoline = forecastMonthEntity.getAmountGasoline();
				
				if(TsBqUtils.isNotNull(forecastValue)){
					forecastValueBd = forecastValueBd.add(new BigDecimal(forecastValue));
				}
				
				if(TsBqUtils.isNotNull(gatherValue)){
					gatherValueBd = gatherValueBd.add(new BigDecimal(gatherValue));
				}

				if(TsBqUtils.isNotNull(amountDiesel)){
					amountDieselBd = amountDieselBd.add(new BigDecimal(amountDiesel));
				}
				
				if(TsBqUtils.isNotNull(amountGasoline)){
					amountGasolineBd = amountGasolineBd.add(new BigDecimal(amountGasoline));
				}
				
			}
			
			entity.setAmountGasoline(amountGasolineBd.toString());
			entity.setAmountDiesel(amountDieselBd.toString());
			entity.setForecastValue(forecastValueBd.toString());
			entity.setGatherValue(gatherValueBd.toString());
			
			entity.setYearMonths(gasStationinfoForecastMonth.getTime());
			
			
		}
		
		return DataResult.success(iPage);
	}
	
	
}
